//
//  UserManagerView.m
//  SZBank
//
//  Created by LuoYao on 16/7/9.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "UserManagerView.h"
#import "LYPersonDetailView.h"
#import "EmailListView.h"
#import "LYChangePasswordView.h"
#import "LYSettingView.h"
#import "LYChangeHeaderImageView.h"

@interface UserManagerView ()

@property (nonatomic) UIButton *headerBtn;/**< 头像 */
@property (nonatomic) UILabel *userNameLabel;/**< 用户名及欢迎语句 */
@property (nonatomic) UIButton *changeHeaderImageBtn;/**< 修改头像 */
@property (nonatomic) UIButton *changeUserPassweodBtn;/**< 修改密码 */

@property (nonatomic) UIImageView *emailImageView;/**< 邮箱 */
@property (nonatomic) EmailListView *emailView;/**< 邮箱 */

@property (nonatomic) LYChangePasswordView *changePasswordView;/**< 修改密码视图 */
@property (nonatomic) LYSettingView *settingView;/**< 设置视图 */


@property (nonatomic) LYChangeHeaderImageView *changeHeaderImageView;/**< 更换头像 */

@end

@implementation UserManagerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //配置用户名
        self.userNameLabel.text = [NSString stringWithFormat:@"欢迎你：%@",[PersonInfoSingle defaultPersonInfo].loginPersonInfo[@"REALNAME"]];
        [self addSubview:self.headerBtn];
        [self addSubview:self.userNameLabel];
        [self addSubview:self.changeHeaderImageBtn];
        [self addSubview:self.changeUserPassweodBtn];
        
        
        //信箱、设置、注销
        NSArray *btnNames = @[@"消息推送_菜单_信箱",@"设置",@"登出"];
        for (int i = 0; i < btnNames.count; i ++) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(KWIDTH * 0.8 + i * KWIDTH * 0.06, self.frame.size.height * 0.5, KWIDTH * 0.03, KWIDTH * 0.03)];
            [btn addTarget:self action:@selector(action_btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn setImage:[LocalDataManager getImageWithSourceOfPath:btnNames[i]] forState:UIControlStateNormal];
            btn.tag = 1000 + i + 4;
            
            [self addSubview:btn];
            
        }
        
        //更换头像通知处理
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"header" object:nil];
        
        //登录成功通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_LoginSuccess) name:@"loginsuccess" object:nil];
        
    }
    return self;
}


#pragma mark - 通知执行方法
- (void)notificationAction:(NSNotification *)notification{
    //从plist文件中获取头像
    NSData *imageData = [LocalDataManager getImageDataWithPlist];
    UIImage *image = [UIImage imageWithData:imageData];
    
    [self.headerBtn setImage:image forState:UIControlStateNormal];
}

//登录成功
- (void)notification_LoginSuccess{
    
    NSString *sex = [[PersonInfoSingle defaultPersonInfo].loginPersonInfo[@"SEX"] isEqualToString:@"女"] ? @"女士" : @"先生";
    
    self.userNameLabel.text = [NSString stringWithFormat:@"欢迎你：%@ %@",[PersonInfoSingle defaultPersonInfo].loginPersonInfo[@"REALNAME"],sex];
    
    
    
}

#pragma mark - 点击事件
- (void)action_btnClick:(UIButton *)sender{
    
    //移除多余视图
//    NSArray *array = self.superview.subviews;
    
    
    switch (sender.tag - 1000) {
        case 1:
        {
            LYPersonDetailView *person = [[LYPersonDetailView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
            [self.superview addSubview:person];
        }
            break;
        
        case 2:
        {//头像修改
            [self.changeHeaderImageView alertChoose:sender];
            
        }
            break;
            
            
        case 3:{
            [self.superview addSubview:self.changePasswordView];
        }
            break;
            
        case 4:
        {
            //邮箱View
            self.emailView = [[EmailListView alloc] initWithFrame:self.superview.bounds];
            [self.superview addSubview:self.emailView];
        }
            break;
            
        case 5:{
            //设置view
            [self.superview addSubview:self.settingView];
        }
            break;
            
        case 6:
        {
            //登出
            [LocalDataManager addAlertControllerWithControllerTitle:@"温馨提示" controllerMessage:@"确定退出" actionSureTitle:@"确定" actionCancelTile:@"取消" actionSureBlock:^(id codes) {
                [self loginOutAction];
            } actionCancelBlock:nil style:sureAndCancel];
        }
            
        default:
            break;
    }
}


#pragma mark 登出方法
- (void)loginOutAction{
    //注销登录状态
    [PersonInfoSingle defaultPersonInfo].loginPersonInfo = nil;
    
    //发通知给根控制器，添加登录界面
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"loginout" object:self];
    
    //退出程序
    exit(0);
    
    
}



#pragma mark - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



#pragma mark - getter
- (UIButton *)headerBtn{
    if (!_headerBtn) {
        _headerBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width * 0.02, self.frame.size.height * 0.3, KWIDTH * 0.04, KWIDTH * 0.04)];
        _headerBtn.tag = 1000 + 1;
        
        
        //从plist文件中获取头像
        NSData *imageData = [LocalDataManager getImageDataWithPlist];
        UIImage *image = [UIImage imageWithData:imageData];
        
        _headerBtn.clipsToBounds = YES;
        _headerBtn.layer.cornerRadius = KWIDTH * 0.02;
        
        [_headerBtn setImage:image forState:UIControlStateNormal];
        [_headerBtn addTarget:self action:@selector(action_btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headerBtn;
}


- (UILabel *)userNameLabel{
    if (!_userNameLabel) {
        _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.headerBtn.frame) + KWIDTH * 0.01, self.frame.size.height * 0.2, KWIDTH * 0.5, self.frame.size.height * 0.3)];
    }
    return _userNameLabel;
}

- (UIButton *)changeHeaderImageBtn{
    if (!_changeHeaderImageBtn) {
        _changeHeaderImageBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.headerBtn.frame) + KWIDTH * 0.01, self.frame.size.height * 0.6, KWIDTH * 0.06, self.frame.size.height * 0.3)];
        [_changeHeaderImageBtn setImage:[LocalDataManager getImageWithSourceOfPath:@"个人信息_拍照_修改头像"] forState:UIControlStateNormal];
        _changeHeaderImageBtn.tag = 1000 + 2;
        [_changeHeaderImageBtn addTarget:self action:@selector(action_btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _changeHeaderImageBtn;
}


- (UIButton *)changeUserPassweodBtn{
    if (!_changeUserPassweodBtn) {
        _changeUserPassweodBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.changeHeaderImageBtn.frame) + KWIDTH * 0.01, self.frame.size.height * 0.6, KWIDTH * 0.06, self.frame.size.height * 0.3)];
        _changeUserPassweodBtn.tag = 1000 + 3;
        [_changeUserPassweodBtn setImage:[LocalDataManager getImageWithSourceOfPath:@"修改密码"] forState:UIControlStateNormal];
        [_changeUserPassweodBtn addTarget:self action:@selector(action_btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _changeUserPassweodBtn;
}

- (LYChangePasswordView *)changePasswordView{
    if (!_changePasswordView) {
        _changePasswordView = [[LYChangePasswordView alloc] initWithFrame:self.superview.bounds];
    }
    return _changePasswordView;
}

- (LYSettingView *)settingView{
    if (!_settingView) {
        _settingView = [[LYSettingView alloc] initWithFrame:self.superview.bounds];
    }
    return _settingView;
}


- (LYChangeHeaderImageView *)changeHeaderImageView{
    if (!_changeHeaderImageView ) {
        _changeHeaderImageView = [[LYChangeHeaderImageView alloc] initWithFrame:self.bounds];
    }
    return _changeHeaderImageView;
}

@end
